# -*- coding: utf-8 -*-
''' Interconversion between CHS and CHT. 
    Include file_conversion and string_conversion
    Reference: http://code.google.com/p/pyjft/; http://code.google.com/p/pyzh/
'''

import os
import cPickle

def __get_chinese():
    tmp_dict = {}
    f = open(os.path.dirname(__file__) + 'CHS_CHT_Room.txt', 'rb')
    tmp_dict = cPickle.load(f)
    f.close()
    return tmp_dict
    
CHST = __get_chinese()  # Chinese unicode encoding.

def CHT_to_CHS(sourceString):
    '''
    Traditional Chinese character to simplified Chinese character converter

    Parameters:
        sourceString : The string to be converted

    Return value:
        The output simplified Chinese character string
    '''
    des = []
    nString = sourceString.encode('gbk')
    sLen = len(nString)
    founded = 0#
    pos = CHST['S_Chinese'].find(nString)
    while pos>0:
        if pos%2 == 0:
            break
        pos = CHST['S_Chinese'].find(nString,pos+1)
    if pos>0:
        pos2 = CHST['T_Chinese'].find(nString)
        while pos2>0:
            if pos2%2 == 0 and pos2 != pos:
                des.append(unicode(CHST['S_Chinese'][pos2:pos2+2],'gbk').encode("utf8"))
                founded = 1
                break
            pos2 = CHST['T_Chinese'].find(nString,pos2+1)
        if not founded:
            des.append(unicode(CHST['S_Chinese'][pos:pos+2],'gbk').encode("utf8"))
    return ''.join(des)

def CHS_to_CHT(sourceString):
    '''
    Traditional Chinese character to simplified Chinese character converter

    Parameters:
        sourceString : The string to be converted

    Return value:
        The output simplified Chinese character string
    '''
    des = []
    nString = sourceString.encode('gbk')
    pos = CHST['S_Chinese'].find(nString)
    while pos>0:
        if pos%2 == 0:
            des.append(unicode(CHST['T_Chinese'][pos:pos+2],'gbk').encode("utf8"))
            break
        pos = CHST['S_Chinese'].find(nString,pos+1)
    return ''.join(des)

def file_conversion(file1, file2, s = 0):
    '''

    Parameters:
        file1 : source file
        file2 : target file
        s: 0 CHS_to_CHT; 1 CHT_to_CHS

    '''
    targetfile = open(file2, "w")
    sourcefile = open(file1, "r")
    k=0
    l = ""  # one line
    ll=[]   # lines
    for i in sourcefile.readlines():
        l = ""
        for j in i.decode("utf8"):
            if j >= u'\u4e00' and j <= u'\u9fa5':
                if not s:
                    a = CHS_to_CHT(j)
                else:
                    a = CHT_to_CHS(j)
                l = l+a
            else:
                l= l+j.encode("utf8")
        ll.append(l)
    targetfile.writelines(ll)
    targetfile.close()
    sourcefile.close()
    
def string_conversion(sourcestring, s = 0):
    '''

    Parameters:
        sourcestring : sourceString : The string to be converted
        s: 0 CHS_to_CHT; 1 CHT_to_CHS

    '''
    l = ""
    for j in sourcestring.decode("utf8"):
        if j >= u'\u4e00' and j <= u'\u9fa5':
            if not s:
                print 1
                a = CHS_to_CHT(j)
            else:
                a = CHT_to_CHS(j)
            l = l+a
        else:
            l= l+j.encode("utf8")
    return l

if __name__ == '__main__':
    #file_conversion("abc.txt.copy", "abc.txt", 1)
    #print string_conversion("fdsaf哲学")

